﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls.Base;

namespace Silvermoon.Controls
{
    /// <summary>
    /// Control that implements <see cref="INavigationNode"/> and returns itself as INavigationNode.Control.
    /// </summary>
    public class NavigationNode : Decorator, INavigationNode
    {
        #region fields

        private NavigationNode previous;
        private NavigationNode next;

        #endregion
        #region dtor

        public NavigationNode()
            : base()
        {
            Margin = Margin.Zero;
        }

        protected override void Dispose(bool dispose)
        {

            NavigationNode prev = previous;
            if (prev != null)
            {
                previous = null;
                prev.Next = null;
                prev.Dispose();
            }
            NavigationNode ne = next;
            if (ne != null)
            {
                next = null;
                ne.Previous = null;
                ne.Dispose();
            }
            base.Dispose(dispose);
        }

        #endregion
        #region props

        /// <summary>
        /// Gets or sets the previous node.
        /// <value></value>
        public NavigationNode Previous
        {
            get { return previous; }
            set
            {
                if (previous != null) previous.next = null;
                previous = value;
                if (previous != null) previous.next = this;
            }
        }

        /// <summary>
        /// Gets or sets the next node.
        /// </summary>
        /// <value></value>
        public NavigationNode Next
        {
            get { return next; }
            set
            {
                if (next != null) next.previous = null;
                next = value;
                if (next != null) next.previous = this;
            }
        }

        #endregion
        #region INavigation Members

        INavigationNode INavigationNode.Previous
        {
            get { return previous; }
        }

        INavigationNode INavigationNode.Next
        {
            get { return next; }
        }

        #endregion
    }
}
